#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson
import json

from model import StudentVoice
from model import ClassState

class MyHandler(webapp.RequestHandler):
    
    def get(self):
        voiceCount = [];
        for i in range(10):
            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
            voice = db.Query(StudentVoice).filter('when > ', plus_one_minutes_ago).filter('when < ', x_minutes_ago).count()
            voiceCount.append(voice)
            
        # change! - modify by class id.  
        numOfStuds=db.Query(StudentVoice).count()
            
        # change! - get real subjects
        classState=db.Query(ClassState)
        db.delete(classState)
        classState=classState.fetch(limit=1)
        
        subjectIndex=0
        if len(classState)==1:
            subjectIndex=classState[0].currentSubjectIndex
                
        dirResponse=dict(studentCols=voiceCount,numOfStudents=numOfStuds,subjectInd=subjectIndex);    
        
        self.response.out.write(json.dumps(dirResponse))
                            


def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

##!/usr/bin/env python
#
#import wsgiref.handlers
#from google.appengine.ext import db
#from google.appengine.ext import webapp
#from google.appengine.ext.webapp import template
#from django.utils import simplejson
#import datetime
#from model import Voice
#
#class MyHandler(webapp.RequestHandler):
#    def get(self):
#        self.response.out.write("test");
#        self.
##        self.response.out.write("json page works");
#        voiceCount = ();
#        for i in range(10):
#            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
#            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
#            voiceCount += (simplejson.loads(
#                        db.Query(Voice)
#                        .filter('when > ', plus_one_minutes_ago)
#                        .filter('when < ', x_minutes_ago)
#                        .count()) , );
##                        
##        
##        #self.response.out.write(simplejson.dumps(voiceCount));